package com.example.fileup;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Service
public class FileUpService {
	public String fileUpload(MultipartHttpServletRequest mRequest){
		String img = "";
		
		String uploadPath = "";
		File dir = new File(uploadPath);
		Iterator<String> iter = mRequest.getFileNames();
		while(iter.hasNext()) {
			String uploadFileName = iter.next();
			
			MultipartFile mFile = mRequest.getFile(uploadFileName);
			String originalFileName = mFile.getOriginalFilename();
			String saveFileName = originalFileName;
			
			if(saveFileName != null && !saveFileName.equals("")) {
				if(new File(uploadPath + saveFileName).exists()) {
					saveFileName = saveFileName + "_" + System.currentTimeMillis();
				}//end if
				
				try {
					mFile.transferTo(new File(uploadPath + saveFileName));
					img = originalFileName;
				}  catch (Exception e) {
					e.printStackTrace();
				}//try ~catch
			} // if end
		} // while end
		
		return img;		
	}//fileUpload()
	
}//class
